﻿using DataConverter.Utils;
using QDAS;
using QDasConverter;
using System.Text;


namespace DataConverter.Core
{
    public class C2021T09_Excel : ConvertBase
    {
        /// <summary>
        /// 数据目录。
        /// </summary>
        public string DataDirectory = @"C:\data\Kisler.DIFF0030";

        public override void Initialize()
        {
            base.Initialize();
            NeedConnectionCheck = false;
            //DataDirectory = ia.ReadString("DataPath", DataDirectory);
        }


        public override void OneLoop_Process()
        {
            //DataDirectory = @"C:\Users\10768\excel";;
            DataDirectory = @"..\..\..\Data\";
            foreach (var xlsfile in Directory.GetFiles(DataDirectory))
            {
                
                var sheet = ExcelHelper.LoadWorkSheet(xlsfile, 0);
                if (sheet == null)
                    continue;

                //获取文件名称 DCT-0180-1
                string path = Path.GetFileNameWithoutExtension(xlsfile);

                var strs = path.Split('-');

                QFile qf = new QFile();
                qf[0100] = sheet["C5"].Value;

                if (strs.Length == 3)
                {
                    //DCT-0180-1.xls
                    qf[1001] = strs[0] + "-" + strs[1];
                } else{
                    qf[1001] = path.Replace(".xls","");
                }

                qf[1002] = path.Replace(".xls", "");

                var qc1 = qf.AddQCharacteristic();
                qc1[2001] = "1";
                qc1[2002] = sheet["D4"].Value;


                var qc2 = qf.AddQCharacteristic();
                qc2[2001] = "2";
                qc2[2002] = sheet["E4"].Value;


                var qc3 = qf.AddQCharacteristic();
                qc3[2001] = "3";
                qc3[2002] = sheet["F4"].Value;

                var qc4 = qf.AddQCharacteristic();
                qc4[2001] = "4";
                qc4[2002] = sheet["G4"].Value;

                var dt = DateTime.Now;

                int i = 0;
                foreach (var row in sheet.Rows)
                {
                    if (i > 4)
                    {
                        dt = DateTime.Parse(row["B" + i].Value.ToString());
                        var qd1 = qc1.AddItem();
                        qd1.SetValue(row["D" + i].Value.ToString());
                        qd1.date = dt;
                        qd1[0014] = row["A" + i].Value.ToString();
                        qd1[1002] = row["C" + i].Value.ToString();

                        var qd2 = qc2.AddItem();
                        qd2.SetValue(row["E" + i].Value.ToString());
                        qd2.date = dt;

                        var qd3 = qc3.AddItem();
                        qd3.SetValue(row["F" + i].Value.ToString());
                        qd3.date = dt;

                        var qd4 = qc4.AddItem();
                        qd4.SetValue(row["G" + i].Value.ToString());
                        qd4.date = dt;
                    }
                    i++;
                }
                qf.ToDMode();


                SaveToDFQ(qf, OutputDirectory, $"C098_{qf[0100]}_{dt:yyyyMMddHHmmss}_{NowString()}.dfq");
            }
            base.OneLoop_Process();
        }
    }
}
